<?php
/**
 * 对数据进行过滤和检查
 * Enter description here ...
 * @author Code.404
 *
 */
class FAC {
	const STRING 			= 0;
	const INT 				= 1;
	const EMAIL 			= 2;
	const PASSWORD 			= 3;
	const USERNAME 			= 4;
	const TEL 				= 5;
	const URL				= 6;
	const PRIORITY			= 7;
	const URL_SUFFIX		= 8;
	const BOOL_VALUE		= 9;
	
	/**
	 * 内容过滤
	 * Enter description here ...
	 * @param unknown_type $data
	 * @param unknown_type $type
	 * @param unknown_type $min_len
	 * @param unknown_type $max_len
	 * @param unknown_type $force	是否强制转换（在出错时将不抛出异常，而是强制显示）
	 */
	static function F($data ,$type = self::STRING ,$min_len = null , $max_len = null , $force = false){
		try{
			$data = self::C($data , $type , $min_len , $max_len );
		}catch (ThinkException $e){
			if (!$force){
				throw_exception($e->getMessage());
			}
		}
		
		return Input::getVar($data);
	}
	/**
	 * 内容检查
	 * Enter description here ...
	 * @param unknown_type $data
	 * @param unknown_type $type
	 * @param unknown_type $min_len
	 * @param unknown_type $max_len
	 */
	static function C($data, $type = self::STRING ,$min_len = null , $max_len = null ){
		$len = mb_strlen($data, 'UTF-8');
		if (!is_null($min_len)){
			$len < $min_len && throw_exception('长度错误');
		}
		if(!is_null($max_len)){
			$len > $max_len && throw_exception('长度错误');
		}
		
		switch ($type){
			case self::INT:
				if(!is_numeric($data)){
					throw_exception('不是一个合法的数字');
				}
				break;
			case self::STRING:
				
				break;
			case self::EMAIL:
				break;
			case self::PASSWORD:
				if($len < 5 || $len > 50){
					throw_exception('密码不合法');
				}
				return sha1($data);
			case self::USERNAME:
				if($len < 5 || $len > 20){
					throw_exception('用户名不合法');
				}
				break;
			case self::TEL:
				break;
			case self::URL:
				if($len < 2 || $len > 100){
					throw_exception('链接地址长度不合法');
				}
				break;
			case self:PRIORITY:
				if(!is_numeric($data) || $data < 0 || $data > 99){
					throw_exception('优先级别错误！');
				}
				break;
			case self::URL_SUFFIX:
				if($data[0] != '.')
					return '.' . $data;
				break;
			case self::BOOL_VALUE:
				return ($data == 'true' || $data == 1 || $data === true ) ? true : false;
			default:
		}
		return $data;
	}
	
}